/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.util.logsystem;


/**
 * LevelManager.
 *
 * @author Sowrov
 * @version %I% %G%
 */
public class LevelManager {
    //current level holder
    LogLevel currentLevel;

    /**
     * Default constructor of level manager, initialize the level up to DEBUG
     */
    LevelManager() {
        this.currentLevel = LogLevel.DEBUG;
    } // end LevelManager()

    /**
     * Constructor of level manager, initialize the level up to given value
     *
     * @param level
     */
    LevelManager(final LogLevel level) {
        this.currentLevel = level;
    } // end LevelManager()

    /**
     * Is the given level is enabled by the current level
     *
     * @param level given level
     *
     * @return true/false base one enabled or disabled
     */
    boolean isEnableLevel(final LogLevel level) {
        if (level.compareTo(LogLevel.NOLOG) > 0) {
            return level.compareTo(this.currentLevel) <= 0; //less than or equal
        } // end if

        return false;
    } // end isEnableLevel()

    /**
     * set level for the current logger
     *
     * @param level
     */
    void setLevel(final LogLevel level) {
        this.currentLevel = level;
    } // end setLevel()

    /**
     * return current log level
     *
     * @return current log level
     */
    LogLevel getLevel() {
        return this.currentLevel;
    } // end getLevel()

    /**
     * convert a level to sting name.
     *
     * @param level given level to convert
     *
     * @return "Unknown" if the level is grater than 5
     */
    static String getLevelName(final LogLevel level) {
        return level.name();
    } // end getLevelName()

    /**
     * if levelName is not from LogLevel enum list then it will be by default DEBUG Level
     *
     * @param levelName DEBUG, WARNING, CRITICAL.. etc
     *
     * @return LogLevel
     */
    static LogLevel findLogLevel(final String levelName) {
        LogLevel logLevel;

        try {
            logLevel = LogLevel.valueOf(levelName);
        } // end try
        catch (final IllegalArgumentException exp) {
            logLevel = LogLevel.DEBUG; //default
        } // end catch

        return logLevel;
    } // end findLogLevel()
} // end LevelManager
